#include "gamescene.h"

#include <game/player.h>
#include <game/game.h>

#include <QtOpenGL>

GameScene::GameScene(Game *game, QObject *parent) :
    PaintScene(parent)
{
    myGame = game;
    firstTime = true;
    connect(myGame, SIGNAL(updated()), this, SIGNAL(updated()));
}

void GameScene::paint(int width, int /*height*/, QGLWidget *widget) {
    if (firstTime) {
        firstTime = false;
        earthTexture = widget->bindTexture(QPixmap(":/images/map.png"), GL_TEXTURE_2D);
        sunTexture = widget->bindTexture(QPixmap(":/images/sun.png"), GL_TEXTURE_2D);
        marsTexture = widget->bindTexture(QPixmap(":/images/mars.png"), GL_TEXTURE_2D);
        starTexture = widget->bindTexture(QPixmap(":/images/Star.png"), GL_TEXTURE_2D);
        redTexture = widget->bindTexture(QPixmap(":/images/red.png"), GL_TEXTURE_2D);
        greenTexture = widget->bindTexture(QPixmap(":/images/green.png"), GL_TEXTURE_2D);
        shipTexture = widget->bindTexture(QPixmap(":/images/kspaceduel.png"), GL_TEXTURE_2D);
        fireTexture = widget->bindTexture(QPixmap(":/images/fire.png"), GL_TEXTURE_2D);
        whiteTexture = widget->bindTexture(QPixmap(":/images/white.png"), GL_TEXTURE_2D);
        planetTexture = widget->bindTexture(QPixmap(":/images/planet.png"), GL_TEXTURE_2D);
        bulletTexture = widget->bindTexture(QPixmap(":/images/bullet.png"), GL_TEXTURE_2D);
        backTexture = widget->bindTexture(QPixmap(":/images/stars.png"), GL_TEXTURE_2D);
    }
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		// Clear The Screen And The Depth Buffer

    widget->qglColor(QColor(255, 255, 255, 255));
    glLoadIdentity();
    //glTranslated(0.0, 0.0, -2000.0 * zoomFactor);
    glTranslated(0.0, 0.0, -2.0);
    //drawTexture(QRectF(0, 0, 1280, 800), backTexture);

//    renderText(400, 400, 0, "hello");

    QVector<Player *> players = myGame->getPlayers();
    if (myGame->getState() == "game over") {
        foreach (Player *pl, players) {
            if (pl->isAlive()) {
                QString s = QString("Player %1 wins!").arg(pl->getName());
                //painter.drawText(QRectF(0, 0, 1280, 800), Qt::AlignCenter, s);
                widget->renderText(640, 400, s);
            }
        }
    }
    PWorld *world = myGame->getWorld();
    glBindTexture(GL_TEXTURE_2D, starTexture);
    foreach (PObject *obj, world->getObjects()) {
        if (obj->getType() == "ship") {
            continue;
        }
        //makeBall(obj->getX(), 800 - obj->getY(), 0, obj->getR(), 10);
        glTranslated(obj->getX(), obj->getY(), 0);
        double r = obj->getR();

        if (obj->getType() == "star") {
            widget->drawTexture(QRectF(-r, -r, 2 * r, 2 * r), starTexture);
        } else if (obj->getType() == "planet") {
            widget->drawTexture(QRectF(-r, -r, 2 * r, 2 * r), planetTexture);
        } else if (obj->getType() == "bullet") {
            widget->drawTexture(QRectF(-r, -r, 2 * r, 2 * r), bulletTexture);
        }

        glTranslated(-obj->getX(), -obj->getY(), 0);
    }
    if (players.size() < 2 || !players[0]->isAlive() || !players[1]->isAlive()) {
        return;
    }
    foreach (Player *pl, players) {
        if (!pl->isAlive() || pl->getShip() == 0) {
            //continue;
            return;
        }

        int x = pl->getShip()->getX();
        int y = pl->getShip()->getY();
        int r = pl->getShip()->getR();
        double alpha = pl->getShip()->getAlpha();

        glTranslated(x, y, 0);
        glRotated(-alpha * 180 / 3.14, 0, 0, -1);

        widget->qglColor(pl->getColor());

        widget->drawTexture(QRectF(-r, -r, 2 * r, 2 * r), whiteTexture);

        widget->drawTexture(QRectF(-r, -r, 2 * r, 2 * r), shipTexture);

        widget->qglColor(QColor(255, 255, 255, 255));

        if(pl->getShip()->isAccelerating()) {
            widget->drawTexture(QRectF(-r, -r, 2 * r, 2 * r), fireTexture);
        }

        glRotated(alpha * 180 / 3.14, 0, 0, -1);
        glTranslated(-x, -y, 0);
    }
    widget->qglColor(QColor(255, 255, 255, 255));
    if (players.size() == 2) {
        PShip *ship1 = players[0]->getShip();
        PShip *ship2 = players[1]->getShip();

        int life = (width / 2 - 20) * ship1->getLife() / 100;
        int energy = (width / 2 - 20) * ship1->getEnergy() / ship1->getMaxEnergy();
        int life2 = (width / 2 - 20) * ship2->getLife() / 100;
        int energy2 = (width / 2 - 20) * ship2->getEnergy() / ship1->getMaxEnergy();
        if (!players[0]->isAlive()) {
            life = 0;
            energy = 0;
        }
        if (!players[1]->isAlive()) {
            life2 = 0;
            energy2 = 0;
        }

        widget->drawTexture(QRectF(10, 800 - 3 - 10, life, 3), greenTexture);

        widget->drawTexture(QRectF(width/ 2 + 10, 800 - 3 - 10, life2, 3), greenTexture);

        widget->drawTexture(QRectF(10, 800 - 3 - 15, energy, 3), redTexture);

        widget->drawTexture(QRectF(width/ 2 + 10, 800 - 3 - 15, energy2, 3), redTexture);
    }
}
